<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class LoginRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'user_name' => 'required|max:50',
            'password' => 'required',
        ];
    }

    /**
     * 验证提示信息
     * @return array
     * @Author 姿势就是力量
     */
    public function messages()
    {
        return [
            'user_name.required' => '用户名不能为空',
            'user_name.max' => '用户名长度只能在20个字符之内',
            'password.required' => '密码不能为空',
        ];
    }


    protected function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(response()->json([
            'code' => 0,
            'data' => [],
            'msg' => $validator->errors()->first()
        ], 400));
    }
}
